package com.ptszyxx.popose.module.main.tea;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.github.gzuliyujiang.wheelpicker.OptionPicker;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentTeaReleaseBinding;
import com.ptszyxx.popose.module.main.tea.vm.TeaReleaseVM;
import com.ysg.base.BaseFragment;
import com.ysg.callback.StringCallback;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.utils.YDialogUtil;

import java.util.Arrays;
import java.util.List;

public class TeaReleaseFragment extends BaseFragment<FragmentTeaReleaseBinding, TeaReleaseVM> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_tea_release;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public TeaReleaseVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(TeaReleaseVM.class);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onPersonEvent.observe(this, o -> showDialogPerson());
        viewModel.uc.onTimeEvent.observe(this, o -> showDialogTime());
        viewModel.uc.onHeatEvent.observe(this, o -> showDialogHeat());
        viewModel.uc.onSweetEvent.observe(this, o -> showDialogSweet());
        viewModel.uc.onCategorizeEvent.observe(this, o -> showDialogCategorize());
    }



    /**
     * 活动对象
     */
    private void showDialogPerson() {
        List<String> list = Arrays.asList(getResources().getStringArray(R.array.tea_list_person));
        OptionPicker picker = new OptionPicker(getActivity());
        picker.setData(list);
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                viewModel.person.set(list.get(position));
            }
        });
        picker.show();
    }
    /**
     * 活动时间
     */
    private void showDialogTime() {
        YDialogUtil.getInstance().showDialogDateTime(getActivity(), new StringCallback() {
            @Override
            public void onResult(String s) {
                viewModel.time.set(s);
            }
        });
    }

    /**
     * 温度
     */
    private void showDialogHeat() {
        List<String> list = Arrays.asList(getResources().getStringArray(R.array.tea_list_heat));

        OptionPicker picker = new OptionPicker(getActivity());
        picker.setData(Arrays.asList(getResources().getStringArray(R.array.tea_list_heat)));
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                viewModel.heat.set(list.get(position));
            }
        });
        picker.show();
    }
    /**
     * 甜度
     */
    private void showDialogSweet() {
        List<String> list = Arrays.asList(getResources().getStringArray(R.array.tea_list_sweet));
        OptionPicker picker = new OptionPicker(getActivity());
        picker.setData(list);
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                viewModel.sweet.set(list.get(position));
            }
        });
        picker.show();
    }
    /**
     * 活动分类
     */
    private void showDialogCategorize() {
        List<String> list = Arrays.asList(getResources().getStringArray(R.array.tea_list_categorize));
        OptionPicker picker = new OptionPicker(getActivity());
        picker.setData(list);
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                viewModel.categorize.set(list.get(position));
            }
        });
        picker.show();
    }
}
